package com.shengde.jinwawaforleader.model.myJob.classList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.base.CommonBaseAdapter;
import com.shengde.jinwawaforleader.base.ViewHolder;
import com.shengde.jinwawaforleader.bean.TeacherFlagInAllClass;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProAllClassOfSchool;

import java.util.ArrayList;
import java.util.List;

public class ChangeTeacherActivity extends BaseActivity {
    private TextView tv_back;
    private TextView fill_add;
    private TextView tv_title;
    private String name;
    private int teacherid;
    private ListView lv_changeTea;
    private CommonBaseAdapter<ProAllClassOfSchool.ClsEntity> adapter;
    public List<ProAllClassOfSchool.ClsEntity> classlist;
    private ArrayList<TeacherFlagInAllClass> Ids = new ArrayList<>();

    @Override
    public void onRoot(Bundle savedInstanceState) {
        setContentView(R.layout.activity_change_teacher);
        Intent intent = getIntent();
        name = intent.getStringExtra("name");
        teacherid = intent.getIntExtra("teacherid",0);
    }

    @Override
    public void initView() {
        tv_back = (TextView)findViewById(R.id.tv_back);
        fill_add = (TextView)findViewById(R.id.fill_add);
        tv_title = (TextView)findViewById(R.id.tv_title);
        lv_changeTea = (ListView)findViewById(R.id.lv_changeTea);
    }

    @Override
    public void initData() {
        tv_title.setText(name);
        fill_add.setText(getString(R.string.baocun));
        BaseProtocol.getInstance().postAllClassTeacherId(this,teacherid);
    }

    @Override
    public void initListener() {
        tv_back.setOnClickListener(this);
        fill_add.setOnClickListener(this);
    }

    @Override
    public void onWidgetClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.fill_add:
                for(int i = 0; i < classlist.size(); i++) {
                    if("1".equals(classlist.get(i).getFlag())) {
                        TeacherFlagInAllClass tfiac = new TeacherFlagInAllClass();
                        tfiac.classid = classlist.get(i).getCid();
                        tfiac.flag = "1";
                        Ids.add(tfiac);
                    }else if("0".equals(classlist.get(i).getFlag())) {
                        TeacherFlagInAllClass tfiac = new TeacherFlagInAllClass();
                        tfiac.classid = classlist.get(i).getCid();
                        tfiac.flag = "0";
                        Ids.add(tfiac);
                    }
                }
                BaseProtocol.getInstance().postTeaChangeClass(this,teacherid,Ids);
                break;
        }
    }

    public void paddingData(){
        adapter = new CommonBaseAdapter<ProAllClassOfSchool.ClsEntity>(this,classlist,R.layout.item_changetea_class) {
            @Override
            public void convert(ViewHolder holder,int position) {
                TextView tv_class_name = holder.getView(R.id.tv_class_name);
                CheckBox rb_banzhuren = holder.getView(R.id.rb_banzhuren);
                CheckBox rb_fudaolaoshi = holder.getView(R.id.rb_fudaolaoshi);

                rb_banzhuren.setTag(position);
                rb_fudaolaoshi.setTag(position);

                tv_class_name.setText(classlist.get(position).getName());
                if("1".equals(classlist.get(position).getFlag())) {
                    rb_banzhuren.setChecked(true);
                    rb_fudaolaoshi.setChecked(false);
                }else if("0".equals(classlist.get(position).getFlag())) {
                    rb_fudaolaoshi.setChecked(true);
                    rb_banzhuren.setChecked(false);
                }else {
                    rb_banzhuren.setChecked(false);
                    rb_fudaolaoshi.setChecked(false);
                }

                rb_banzhuren.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos_ban = (int) v.getTag();
                        CheckBox cb = (CheckBox) v;
                        if(cb.isChecked()) {
                            classlist.get(pos_ban).setFlag("1");
                        }else {
                            classlist.get(pos_ban).setFlag(null);
                        }
                        adapter.notifyDataSetChanged();
                    }
                });

                rb_fudaolaoshi.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos_fu = (int) v.getTag();
                        CheckBox cb = (CheckBox) v;
                        if(cb.isChecked()) {
                            classlist.get(pos_fu).setFlag("0");
                        }else {
                            classlist.get(pos_fu).setFlag(null);
                        }
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        };
        lv_changeTea.setAdapter(adapter);
    }
}